package dt.board.review;

import com.opensymphony.xwork2.ActionSupport;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import dt.board.review.pagingAction;

import java.util.*;
import java.io.Reader;
import java.io.IOException;
import java.net.*;

import org.apache.struts2.interceptor.SessionAware;

public class listAction extends ActionSupport implements SessionAware{

	public static Reader reader;
	public static SqlMapClient sqlMapper;
	private Map session;
	
	private List<boardVO> list = new ArrayList<boardVO>();
	
	
	private String searchKeyword;
	private int searchNum;
	

	private int currentPage = 1;
	private int totalCount;
	private int blockCount = 10;
	private int blockPage = 5;
	private String pagingHtml;
	private pagingAction page;
	private int num = 0;
	private int BoardFlag = 5;
	
	private int adminCheck;
	private String adminId; 


	public listAction() throws IOException
	{
		reader = Resources.getResourceAsReader("sqlMapConfig.xml");
		sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
		reader.close();		
		
	}
	
	public String execute() throws Exception {
		
		if(getSearchKeyword() != null)
		{
			return search();
		}
		
		list = sqlMapper.queryForList("review.selectAll");
		
		totalCount = list.size();
		page = new pagingAction(currentPage, totalCount, blockCount, blockPage, num, "");
		pagingHtml = page.getPagingHtml().toString();
		
		int lastCount = totalCount;
		
		if(page.getEndCount() < totalCount)
			lastCount = page.getEndCount() + 1;
		
		
		list = list.subList(page.getStartCount(), lastCount);
		
		adminId = (String)session.get("id"); 
		
		
		if(session.get("id") != null){
			
			if(session.get("id").equals("admin1")){
				
				adminCheck =1;
				
			}
			
		}
		
		return SUCCESS;
	}
	
	public String search() throws Exception {
		
		//searchKeyword = new String(searchKeyword.getBytes("iso-8859-1"),"euc-kr") ;
		//System.out.println(searchKeyword);
		//System.out.println(searchNum);
		if(searchNum == 0){
			list = sqlMapper.queryForList("review.selectSearchW", "%"+getSearchKeyword()+"%");
		}
		if(searchNum == 1){
			list = sqlMapper.queryForList("review.selectSearchS", "%"+getSearchKeyword()+"%");
		}
		if(searchNum == 2){
			list = sqlMapper.queryForList("review.selectSearchC", "%"+getSearchKeyword()+"%");	
		}
		
		totalCount = list.size(); //리스트에 들어있는 총 갯수를 토탈카운트로 설정
		page = new pagingAction(currentPage, totalCount, blockCount, blockPage, num, ""); 
		pagingHtml = page.getPagingHtml().toString(); 
		
		int lastCount = totalCount;
		
		if(page.getEndCount() < totalCount)
			lastCount = page.getEndCount() + 1;
		
		list = list.subList(page.getStartCount(), lastCount); //해당 페이지에서 보여줄 첫번쨰번호와 마지막 글번호를 구해서 10개만 가져온다.
		
		adminId = (String)session.get("id"); 
		
//		세션에 아이디가 존재한다면 그 아이디가 관리자 아이디인지 확인하고 관리자 아이디라면 adminCheck에 값 1을 준다.
		if(session.get("id") != null){
			
		
		     if(session.get("id").equals("admin1")){
			
		            	adminCheck = 1;
			
		}
	}	
		
		return SUCCESS;
	}



	public List<boardVO> getList() {
		return list;
	}

	public void setList(List<boardVO> list) {
		this.list = list;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getBlockCount() {
		return blockCount;
	}

	public void setBlockCount(int blockCount) {
		this.blockCount = blockCount;
	}

	public int getBlockPage() {
		return blockPage;
	}

	public void setBlockPage(int blockPage) {
		this.blockPage = blockPage;
	}

	public String getPagingHtml() {
		return pagingHtml;
	}

	public void setPagingHtml(String pagingHtml) {
		this.pagingHtml = pagingHtml;
	}
	
	
	
	public String getSearchKeyword() {
		return searchKeyword;
	}
	
	public void setSearchKeyword(String searchKeyword) {
		this.searchKeyword = searchKeyword;
	}

	public int getSearchNum() {
		return searchNum;
	}

	public void setSearchNum(int searchNum) {
		this.searchNum = searchNum;
	}
	
	public int getBoardFlag() {
		return BoardFlag;
	}

	public void setBoardFlag(int BoardFlag) {
		this.BoardFlag = BoardFlag;
	}

	@Override
	public void setSession(Map session) {
		// TODO Auto-generated method stub
		this.session = session;
	}

	public Map getSession() {
		// TODO Auto-generated method stub
		return session;
	}
	
	public int getAdminCheck() {
		return adminCheck;
	}

	public void setAdminCheck(int adminCheck) {
		this.adminCheck = adminCheck;
	}

	public String getAdminId() {
		return adminId;
	}

	public void setAdminId(String adminId) {
		this.adminId = adminId;
	}
	
	
	
	

}
